package com.pet.shop.controller.user;

import com.pet.shop.controller.BaseController;
import com.pet.shop.entity.user.SysUser;
import com.pet.shop.entity.user.UserInfo;
import com.pet.shop.service.IBaseService;
import com.pet.shop.service.user.ISysUserService;
import com.pet.shop.service.user.IUserInfoService;
import com.pet.shop.utils.ApiResultUtil;
import com.pet.shop.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/web/sysUser")
public class SysUserController extends BaseController<SysUser> {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public IBaseService<SysUser> getBaseService() {
        return sysUserService;
    }

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request){
        return new ModelAndView("manage/sysUserList");
    }

    @RequestMapping("updatePassword")
    @ResponseBody
    public Map<String, Object> updatePassword(HttpServletRequest request, String password){
        if(StringUtils.isEmpty(password)){
            return ApiResultUtil.error_param("密码不能为空");
        }
        SysUser sessionUser = getSessionUser(request);
        SysUser sysUser = new SysUser();
        sysUser.setPassword(MD5Util.md5(password));
        sysUser.setId(sessionUser.getId());
        sysUser.setModifierId(sessionUser.getId());
        sysUser.setModifyTime(new Date());
        sysUserService.updateEntity(sysUser);
        return ApiResultUtil.success("ok");
    }

    @Override
    public Map<String, Object> removeEntity(HttpServletRequest request, Long id) {
        if(getSessionUser(request).getId().equals(id)){
            return ApiResultUtil.error_param("不能删除自己");
        }
        return super.removeEntity(request, id);
    }
}
